package kltn.audioguidetravel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import kltn.business.BAInfoTravel;
import kltn.data.InfoTravel;
import android.R.drawable;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class PlayActivity extends Activity{

	public TextView songName,startTimeField,endTimeField;
	   private MediaPlayer mediaPlayer;
	   private double startTime = 0;
	   private double finalTime = 0;
	   private Handler myHandler = new Handler();
	   private SeekBar seekbar;
	   private ImageButton playButton;
	   public static int oneTimeOnly = 0;
	   ArrayList<InfoTravel> travels;
	   int direcID;
	   int currentSongIndex = 0;
	   ImageView ivHinh;
	   
	   @Override
	   protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.activity_play);
	      Intent intent=getIntent();
	      direcID=intent.getIntExtra("direcID",0);
	      ArrayList<Integer> IDs=intent.getIntegerArrayListExtra("IDs");
	      BAInfoTravel baInfoTravel= new BAInfoTravel(this);
	      Log.d("test","IDs ="+IDs.size());
	      travels=baInfoTravel.getInfoTravelByArray(IDs, direcID);	      
	      songName = (TextView)findViewById(R.id.tvName);
	      startTimeField =(TextView)findViewById(R.id.tvStart);
	      endTimeField =(TextView)findViewById(R.id.tvEnd);
	      seekbar = (SeekBar)findViewById(R.id.seekBar1);
	      playButton = (ImageButton)findViewById(R.id.ibtPlay);
	      mediaPlayer = new MediaPlayer();
	      ivHinh=(ImageView) findViewById(R.id.ivHinh);
	      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				if(currentSongIndex < (travels.size() - 1)){
					currentSongIndex = currentSongIndex + 1;
					playSong(currentSongIndex);
	            }
			}
		});
	      playSong(0);	      
	      seekbar.setClickable(false);

	   }
	   public void playSong(int index){
		   try {
			   mediaPlayer.reset();
			   mediaPlayer.setDataSource(travels.get(index).getAudio());
			   mediaPlayer.prepare(); // might take long! (for buffering, etc)
			   mediaPlayer.start();
			   playButton.setImageResource(drawable.ic_media_pause);
			   ivHinh.setImageBitmap(travels.get(index).getImg());
			   songName.setText(travels.get(index).getName());
		} catch (IllegalArgumentException | SecurityException
				| IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   finalTime = mediaPlayer.getDuration();
		      startTime = mediaPlayer.getCurrentPosition();
		      if(oneTimeOnly == 0){
		         seekbar.setMax((int) finalTime);
		         oneTimeOnly = 1;
		      } 
		   endTimeField.setText(String.format("%d:%d", 
			         TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
			         TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - 
			         TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
			         toMinutes((long) finalTime)))
			      );
			      startTimeField.setText(String.format("%d:%d", 
			         TimeUnit.MILLISECONDS.toMinutes((long) startTime),
			         TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
			         TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
			         toMinutes((long) startTime)))
			      );
			      seekbar.setProgress((int)startTime);
			      myHandler.postDelayed(UpdateSongTime,100);
			
	   }

	public void play(View view) {
		if (mediaPlayer.isPlaying()){
			Toast.makeText(getApplicationContext(), "Pausing",
					Toast.LENGTH_SHORT).show();
			mediaPlayer.pause();
			playButton.setImageResource(drawable.ic_media_play);
		}else{
			Toast.makeText(getApplicationContext(), "Playing",
					Toast.LENGTH_SHORT).show();
			mediaPlayer.start();
			myHandler.postDelayed(UpdateSongTime, 100);
			playButton.setImageResource(drawable.ic_media_pause);
		}
	}

	   private Runnable UpdateSongTime = new Runnable() {
	      public void run() {
	         startTime = mediaPlayer.getCurrentPosition();
	         startTimeField.setText(String.format("%d:%d", 
	            TimeUnit.MILLISECONDS.toMinutes((long) startTime),
	            TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
	            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
	            toMinutes((long) startTime)))
	         );
	         seekbar.setProgress((int)startTime);
	         myHandler.postDelayed(this, 100);
	      }
	   };
	   public void next(View view){
		   if(currentSongIndex<travels.size()-1){
			   playSong(++currentSongIndex);
		   }
	   }
	   public void previous(View view){
		   if(currentSongIndex>0){
			   playSong(--currentSongIndex);
		   }
	   }
	   

	   @Override
	   public boolean onCreateOptionsMenu(Menu menu) {
	   // Inflate the menu; this adds items to the action bar if it is present.
	   getMenuInflater().inflate(R.menu.main, menu);
	   return true;
	   }
}
